package thinkinjava4.ch13_strings;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TheReplacements {
    public static void main(String[] args) {
        String s = "public class SplitDemo {/*!          geefdfd  /*    d    */           !*/\n" +
                "    public static void main(String[] args) {/*       dfdfdfdfadfd*/\n" +
                "        String input = \"This!!unusual user!!of exclamation!!points\";\n" +
                "        System.out.println(Arrays.toString(Pattern.compile(\"!!\").split(input)));\n" +
                "        //只分隔出前3个\n" +
                "        System.out.println(Arrays.toString(Pattern.compile(\"!!\").split(input, 3)));\n" +
                "    }\n" +
                "}";
        /*DOTALL表示.允许匹配终结符*/
        Matcher mInput = Pattern.compile("/\\*!(.*)!\\*/", Pattern.DOTALL).matcher(s);
        if (mInput.find()) {
            System.out.println("find()");
            s = mInput.group(1);//Captured by parentheses
        }
        // Replace two or more spaces with a single space
        s = s.replaceAll(" {2,}", " ");
        // Replace one or more spaces at the beginning of each
        // line with no spaces. Must enable MULTILINE mode
        s = s.replaceAll("(?m)^ +", "");
        System.out.println(s);
        s = s.replaceFirst("[aeiou]", "(VOWEL1)");
        StringBuffer sBuf = new StringBuffer();
        Pattern p = Pattern.compile("[aeiou]");
        Matcher m = p.matcher(s);
        while (m.find()) {
            m.appendReplacement(sBuf, m.group().toUpperCase());
        }
        m.appendTail(sBuf);
        System.out.println(sBuf);
    }
}
